<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
    function  __construct()
    {
        parent::__construct();
    }
}

class Base extends CI_Controller{

    protected $user;

    public function __construct(){
        parent::__construct();
        $this->load->library('session');
        $this->checkLogin();
        
    }

    private function checkLogin(){
        /* session_start();
         if( !$_SESSION['user'] ){
         $this->load->view('index.html');
         } */
        if(isset($_GET['debug'])){
            $user['uid'] = 1;
            $user['corp_id'] = 1;
            $user['group_id'] = 1;
            $user['user_name'] = 'test';
            $user['photo'] = '';
            $this->session->user = $user;
        }
        if( !$this->session->user ){
            $this->outJsonData(-2, '用户未登录!');
        }
        $this->user = $this->session->user;
    }

    /**
     * 权限校验
     * 判断是否是管理员或者项目成员
     * @param int $project_id
     * @return boolean
     */
    protected function auth_invaid($project_id){
        if( !$project_id ){
            $this->outJsonData(-1, '项目id不能为空');
        }
        //管理员
        if( $this->user['group_id'] == 1 ){
            return true;
        }
        //成员
        $this->load->model('ProjectFollows_model');
        $uids = $this->ProjectFollows_model->getUidsByProjectId($project_id);
        if( in_array($this->user['uid'], $uids) ){
            return true;
        }
        return false;
    }
    
    protected function outJsonData($errno, $msg){
        $data['errorno'] = $errno;
        $data['data'] = $msg;
        echo json_encode($data);
        exit;
    }


    protected function getSafeRequest($key, $default=''){
        if( !is_array($_POST) ){
            $_POST = [];
        }
        if( !is_array($_GET) ){
            $_GET = [];
        }
        $_REQUEST = array_merge($_GET, $_POST);
        if( isset($_REQUEST[$key]) ){
            $str = $_REQUEST[$key];
        } else {
            return $default;
        }
        $str = trim($str);
        $str = htmlspecialchars($str, ENT_QUOTES);
        if (!@get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否打开
            $str = addslashes($str);
        }
        return $str;
    }
    
    protected function getSafePost($key, $default=''){
        if( isset($_POST[$key]) ){
            $str = $_POST[$key];
        } else {
            return $default;
        }
        $str = trim($str);
        $str = htmlspecialchars($str, ENT_QUOTES);
        if (!@get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否打开
            $str = addslashes($str);
        }
        return $str;
    }
    /**
     * 单文件上传
     * @param string $field
     */
    protected function single_upload($field){
        $this->load->library('upload');
        $data = [];
        $this->upload->do_upload($field);
        if( !$this->upload->do_upload($field) ){
            $this->outJsonData(-1, $this->upload->display_errors());
        } else {
            $data = $this->upload->data();
        }
        return $data ? $data : [];
    }
    /**
     * 多文件上传
     */
    protected function multi_upload($field){
        $this->load->library('upload');
        $file = array();
        foreach($_FILES[$field]['name'] as $k=>$v){
            //只取无错误的文件
            if(!empty($_FILES[$field]['name'][$k])){
                $file[] = array(
                    'name' => $_FILES[$field]['name'][$k],
                    'type' => $_FILES[$field]['type'][$k],
                    'tmp_name' => $_FILES[$field]['tmp_name'][$k],
                    'error' => $_FILES[$field]['error'][$k],
                    'size' => $_FILES[$field]['size'][$k],
                );
            }
        }
        if(!$file){
            return [];
        }
        //开始上传多文件
        $data = [];
        foreach($file as $k => $v){
            $temp = [];
            $temp[$field] = $v;
            $_FILES = $temp;
            if( !$this->upload->do_upload($field)) {
                echo $this->upload->display_errors();
                return false;
            } else {
                $data[$field][] = $this->upload->data();
            }
        }
        return $data;
    }
    
}
?>